package com.campus.service.impl;

import com.alibaba.fastjson.JSON;
import com.campus.constant.RoleConstant;
import com.campus.constant.StatusConstant;
import com.campus.context.CurrentHolder;
import com.campus.mapper.HomeworkMapper;
import com.campus.mapper.UserCourseMapper;
import com.campus.pojo.dto.HomeworkDTO;
import com.campus.pojo.dto.HomeworkPageQueryDTO;
import com.campus.pojo.entity.Homework;
import com.campus.pojo.vo.HomeworkVO;
import com.campus.result.PageResult;
import com.campus.service.HomeworkService;
import com.campus.websocket.WebSocketService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HomeworkServiceImpl implements HomeworkService {
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private UserCourseMapper userCourseMapper;
    @Autowired
    private WebSocketService webSocketService;
    @Override
    public PageResult<HomeworkVO> pageQuery(HomeworkPageQueryDTO homeworkPageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(homeworkPageQueryDTO.getCurrentPage(), homeworkPageQueryDTO.getPageSize());

        //2.封装当前用户的id 如果为管理员则置空
        Long userId = CurrentHolder.getCurrentId();
        homeworkPageQueryDTO.setUserId(userId);
        if (userId == RoleConstant.ADMIN_ROLE_ID) {
            homeworkPageQueryDTO.setUserId(null);
        }

        //3.执行查询
        Page<HomeworkVO> page = homeworkMapper.pageQuery(homeworkPageQueryDTO);

        //3.返回结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        homeworkMapper.deleteByIds(ids);
    }

    @Override
    public void save(HomeworkDTO homeworkDTO) {
        //1.构建entity对象-homework
        Homework homework = new Homework();

        //2.属性较多拷贝
        BeanUtils.copyProperties(homeworkDTO, homework);

        //3.封装其他属性 老规矩 即使数据库有默认值 我们业务层设置一下 更规范且易维护
        //状态默认为已发布
        homework.setStatus(StatusConstant.HOMEWORK_STATUS_PUBLISH);
        //发布人默认为1 管理员
        homework.setCreatorId(RoleConstant.ADMIN_ROLE_ID);

        //4.调用mapper方法
        homeworkMapper.insert(homework);

        //5.使用websocket通知选了对应课程的用户
        Long courseId = homework.getCourseId();
        List<Long> userIds = userCourseMapper.selectStudentIdsByCourseId(courseId);

        //包装json 数据
        Map<String, Object> map = new HashMap<>();
        map.put("message", "🔔 作业发布提醒：《" + homework.getTitle() + "》已发布！，请及时查收！");
        map.put("type", 2); //type=1 表示作业截止提醒 type=2 表示作业发布提醒
        String json = JSON.toJSONString(map);

        // 一次性群发
        webSocketService.sendMessageToUsers(userIds, json);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //1.构建entity对象-homework
        Homework homework = Homework.builder()
                .id(id)
                .status(status)
                .build();

        //2.复用update 方法
        homeworkMapper.update(homework);

    }

    @Override
    public void update(HomeworkDTO homeworkDTO) {
        //1.构建entity对象-homework 属性较多采用属性拷贝
        Homework homework = new Homework();
        BeanUtils.copyProperties(homeworkDTO, homework);

        //2.封装其他属性 无需封装其他属性了 userId 仍为1 和 status 仍为 0 不改

        //3.调用mapper方法
        homeworkMapper.update(homework);


    }

    @Override
    public HomeworkVO getById(Long id) {
        return homeworkMapper.getById(id);
    }
}
